package com.zhongtf.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhongtf.utils.BipUtils;
import io.github.novacrypto.bip32.ExtendedPrivateKey;
import io.github.novacrypto.bip32.ExtendedPublicKey;


import java.util.Map;

import static com.zhongtf.utils.BipUtils.getEthAddress;
import static com.zhongtf.utils.RSAUtil.verify;

//交易所，交易所验证票的数据
public class server {
    public static void main(String[] args) {
        //企业票公钥xpub，企业提交给交易所，可以在后台获取
        String publicKeyStr= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIIdFxETeR/L06Yk5sf1F/Q1hC/UDgjOeFWqN9\n" +
                "YAytWjE9OaK+R0AtY0GYDSib6VBKl/5TF4n5aFS4dQMLVoBqlkiGi3MTt2iyXCvvW/kF/rxMD+QI\n" +
                "U2L8QYW7WNJIYHK37esKvF83K0GlJBWGycoB2u3qB3ETTAs7tAv3oyGt8wIDAQAB";
        String appid="1160fc300abce69590556fbb0acec8ca";
        String timestampStr= "1660528869";
        String signature= "Gjjyn5bQq5uJoLhby96givwIHt02MMO4xV77sBTpLqMVtyfOuCjGiKtijgkg/01ZEsXkYCqAbwVhrJjTOoF2NOFKQZIe2wopJrUDv5hXcj1ODBBL/8tauUFvU5e3V8TN3DSiLeCiPAIigW8khv9JcfqsBTIaCua+Mj5ByH6BVAo=";
        //首先用企业公钥对企业传送过来的发票的数据进行验签
        String signData= appid+timestampStr;
        boolean isverify0 =verify(signData, signature, publicKeyStr);
        System.out.println(isverify0 );

        //如果验证签名通过，在对传送过来的每张票进行核对
        //获取xpub，企业票编号为0 m/44'/60'/i'，i表示对应的票的序号
        /**
         * 1.交易所保存企业的每个票的私钥xprv,票根为m/44'/60'/1'/i，i表示每一张票
         *  2.派生路径为m/44'/60'/1'/0对应的xprv私钥Base58字符串，xprv:xprvA1wzUVhL969bgvQc2TKnoajUSPseKUda6DXGESEmA7QsA38GU8MGgbEERT2BQ9Jq2jZotsicGnzUHfhVNUhvRRo3nqxNb7dxjpABmozpyT5
         */
        String priKeyExtendedBase58="xprvA1wzUVhL969bgvQc2TKnoajUSPseKUda6DXGESEmA7QsA38GU8MGgbEERT2BQ9Jq2jZotsicGnzUHfhVNUhvRRo3nqxNb7dxjpABmozpyT5";
        //由私钥字符串priKeyExtendedBase58获取私钥对象
        ExtendedPrivateKey priKey= BipUtils.deserializePrikey(priKeyExtendedBase58);
        //由私钥对象得到相应的公钥对象
        ExtendedPublicKey pubkey =BipUtils.getPubKeyByPrivKey(priKey);
        //公钥对象转换为公钥字符串
        String pubKeyExtendedBase58= BipUtils.getPubKeyExtendedBase58(pubkey);
        //企业传送过来的票据数据
        String data= "{data:{"
                +"1:{\"sequence\":1,\"sign\":\"COfg5ZrikCASyCYfabu7wmHhIevNTpIThluWW+Fmy0aq7bzkoXQym/nrHjtKW3FHljzx2UDHhLulIgoI4uXblIKAD7Pwx0iT4NXM0MBkZRHScDxg5n5Z0O5+alBS7G7zqNDvdQGtP7u86JsojxzRzPeyaj1HSbcTh6qXkwjBxOQ=\",\"token\":\"0xAc85f833BfAC840eF2AAfD6006c5D51d8CF4848E\"},"
                +"2:{\"sequence\":2,\"sign\":\"aaNZt09ChlcggwHYl+omASBsFyjjuPQ3nEuarmMYzR6zFJTKHi5wNLA6Iy/LRxjU77z262kVrGke91ey0ej095HQsg8kKZ4es0puRmHSYbzLazVJKxfuauoMx9MIM44xG5kV79xYNnOUq6LCg3Qg03NUaEET2AZ72YENPeXGABI=\",\"token\":\"0x6A72771cCF1ce5eCd8e10C2a66EC5f7607dAcC1b\"},"
                +"3:{\"sequence\":3,\"sign\":\"Jk2IE7RQsJhEQvfJySker30lhlvhDtMZdu0Fdm4BMIKjulcS9Qc10wVIodfHjuwFCKS9mN3VM4xRdT4k5X9Dz29IRpuEP8IpSHkA7ZDaxZHiXolHc566agbbOTKH1vHFOu6dymzf6z8b2l9y4dGQhUc+oqJJ0W1Rfb+NoHy3AxI=\",\"token\":\"0x0d2539E0fc299965613cD52A8fBe5f1137a01BeD\"},"
                +"4:{\"sequence\":4,\"sign\":\"OGjduE8XnhZzSBGdVF6BvyGKqq4TDa7L7WBeYdQWh2c+o5Smj9B5XGfHFFLgsolLvZa6bJzfdGuu5ds1db5dI3E3DK5CubIv7ivYJwDpMOodjk61m/UWMs2z2LqRoHVFQp5B87wDH7xJkSIveVwMH5c+i/ra+vS/+3S+X89+Gwo=\",\"token\":\"0x6982eCD13f80034823Ed7FBb7139b70D2c7eD737\"},"
                +"5:{\"sequence\":5,\"sign\":\"KoPkMKyOYO/lnoqPV7IGDqYAAyMSoDt3O6w+qMDb1UkudipoBgiq/hdoXvSb/KdRowGftTDFx+FIhDa/KdmISFrcJCYb412KecFW+RBkJmtpageEdvWlL00dEN5M/9ppsmqaZWENl/eokpz0G4BdaDeuSNdVJ2wvF794G4YoGcg=\",\"token\":\"0xBAF5238bbd4A6083345Bb9348f5d22af05eCeb68\"},"
                +"6:{\"sequence\":6,\"sign\":\"E5YVGBU5ZwyDQfv7Q7td2ONi9N4m+QhsALEyh5G4EtlCgdYC3lJckxtNeSBg8tvwJRcfX4RZn0lrSbIHBl/OUd1Xw+d6Xnb3PAgQ8gsMfWlQBtrAWnX7PBLrKYioUxX5QBjsgsOKcADQ+aOmea2nIGXzWzOJ3/kOy6s45nnD2YQ=\",\"token\":\"0xEA60932fdEc4d31D9C244e881514F189A374A2D4\"},"
                +"7:{\"sequence\":7,\"sign\":\"g8Pfn4ENDxXPD1tQlPOofZZSKi8P7dYQsteSP1/GAK1x36LetUe3x0D4yU11wm3W4cpG/8wIkKFoaD6/zFVgHZYy1bnLm4FnnQG9vTx5MFDgnE1VdNbkAsuKk2lou4dL8LG6UjUiSeRgPGPdHPbhzBGExX3Ns+bzfXXjvkFvlHU=\",\"token\":\"0x47AEc4596643f071a4538150D78d680425693ef4\"},"
                +"8:{\"sequence\":8,\"sign\":\"h3o8/2r0OP8kJIjFqMnxh/LCfEWCOW0K2fX/xlXLubs1C7UfkkGHOwpyvM+cMIU3Zer2e6w1Thd2VtmsxwzaxWe3Ww7fN+V8fxx5psF5RkIudEixVumnB2KJwYtXZpYwQUrs79ouAp9iz9aoFMk2qtNsxIBV0MLXKBeggOYQI9E=\",\"token\":\"0xE6d3EB60b7d190EABD0119c11dE66FD27bc642Ee\"},"
                +"9:{\"sequence\":9,\"sign\":\"ddR8FWk2pg7oxyjWeI1S/py9v54/N2NsvSE8J72EmUm0fheufL5eat+0CQz3xmuIK9Zq/4dHL5AmzteiOuDzinLBBJWncIWKj9LlhJOSSVvc3ANjBoiIBYNgXj/T/Wc7kkAlUVAF+vt3yv5DOZTo/0aDtdB3d2ajvIcr67GcIPw=\",\"token\":\"0x951E0D9bA7AAA7B6207fee35Aa2Cf6402aD054F6\"},"
                +"10:{\"sequence\":10,\"sign\":\"BXLEkbh+IpuWJ3tNyBpJu2NmX9cAay5o81V7uod7EHWWLpt/CFEtLzoTY6Ch0JGpiIXgnwecZnWfYEn5uSmH1k2PDGOQWutmcHXgxzyGIiT47H1e8Ia3t6fyIWS+sUf+IpXd0VtJfbgqQLoLYzLujO3Sf29jOfZLOreQw5ieGhk=\",\"token\":\"0x38D211324ff249B22d15edFFFb7fEe2C3aAA0Aeb\"}}}";

        JSONObject tokenObject = JSON.parseObject(data);
        //验证票的ETH地址，假设企业编号为0
        String tokenData =tokenObject.getString("data");
        System.out.println(tokenData );
        String sequence;
        String sign;
        String token;
        String address;
        for (Map.Entry<String, Object> entry: tokenObject.entrySet()) {
            JSONObject o = (JSONObject) entry.getValue();
            for (Map.Entry<String, Object> entry1: o.entrySet()) {
                JSONObject o1 = (JSONObject) entry1.getValue();
                System.out.println(o1);
                sequence= o1.getString("sequence").toString();
                sign= o1.getString("sign").toString();
                token= o1.getString("token").toString();
                //地址检查
                address= getEthAddress(Integer.parseInt(sequence),pubKeyExtendedBase58);
                if(address.equals(token)==false){
                    System.out.println("第:"+sequence+"张票数据地址有问题" );
                    break;
                }
                //每张票的签名验证
                boolean isverify =verify(token, sign, publicKeyStr);

                System.out.println("第:"+sequence+"张票数据验签结果"+isverify );
            }
        }

    }
}
